const app = getApp()
Page({
  data: {
    listKt: [],
    gjz: ''
  },
  onLoad: function(options) {
    const that = this
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Topic').field({
      _id: true,
      ktmc: true,
      teaName: true
    }).limit(20).where({
      dept: app.globalData.userDept,
      ktzt: 0
    }).get().then(res => {
      that.setData({
        listKt: res.data
      })
    })
  },
  onReachBottom: function() {
    const that = this
    const db = wx.cloud.database({
      env: 'gtss-2b1812'
    })
    if (that.data.gjz == '') {
      db.collection('Topic').field({
        _id: true,
        ktmc: true,
        teaName: true
      }).limit(20).skip(that.data.listKt.length).where({
        dept: app.globalData.userDept,
        ktzt: 0
      }).get().then(res => {
        that.setData({
          listKt: that.data.listKt.concat(res.data)
        })
      })
    } else {
      that.setData({
        cxjgksxh: that.data.cxjgksxh + 20
      })
      db.collection('Topic').field({
        _id: true,
        ktmc: true,
        teaName: true
      }).limit(20).skip(that.data.listKt.length).where({
        dept: app.globalData.userDept,
        ktzt: 0,
        ktmc: new db.RegExp({
          regexp: that.data.gjz,
          options: 'i',
        })
      }).get().then(res => {
        that.setData({
          listKt: that.data.listKt.concat(res.data)
        })
      })
    }
  },
  funKtxq: function(e) {
    wx.navigateTo({
      url: "../stuKtxq/stuKtxq?_id=" + e.currentTarget.dataset.id + '&ffrom=stuDs'
    })
  },
  inpKtcx: function(e) {
    const that = this
    that.setData({
      gjz: e.detail.value
    })
    if (e.detail.value == '') {
      wx.cloud.database({
        env: 'gtss-2b1812'
      }).collection('Topic').field({
        _id: true,
        ktmc: true,
        teaName: true
      }).limit(20).where({
        dept: app.globalData.userDept,
        ktzt: 0
      }).get().then(res => {
        that.setData({
          listKt: res.data
        })
      })
    }
  },
  search: function() {
    const that = this
    const db = wx.cloud.database({
      env: 'gtss-2b1812'
    })
    db.collection('Topic').field({
      _id: true,
      ktmc: true,
      teaName: true
    }).where({
      dept: app.globalData.userDept,
      ktzt: 0,
      ktmc: new db.RegExp({
        regexp: that.data.gjz,
        options: 'i',
      })
    }).limit(20).get().then(res => {
      that.setData({
        listKt: res.data
      })
    })
  }
})